//-----------------------------------------------------------------------------------
#ifndef _STATEMACHINE_H_
#define _STATEMACHINE_H_
//-----------------------------------------------------------------------------------
#include <vector>
#include <windows.h>
//-----------------------------------------------------------------------------------
typedef void (*DrawFuncPointer)(void);
typedef bool (*UpdateFuncPointer)(void);
//-----------------------------------------------------------------------------------
typedef struct
{
	float fpProbability;
	unsigned int uiDestinationState;
}sState;
//-----------------------------------------------------------------------------------
class StateMachine
{
public:
	
	StateMachine();
	StateMachine(int stateCount, int eventCount, int initState = 0);
	~StateMachine();

	void InitializeStateMachine(int stateCount, int eventCount, int initState = 0);

	void SetRelation(int state, int event, int nextState, float fpProbab = 100.0f)
	{	
		sState *sTempState = new sState();

		sTempState->uiDestinationState = nextState;
		sTempState->fpProbability = fpProbab;
			
		_fsm[state][event].push_back(sTempState);		
	}

	void OrderByProb(std::vector<sState*> *vFsmVector)
	{

	}

	void SetEvent(int event)
	{ 
		if(_fsm[_state][event].size() > 0) 
		{
			int iRand = rand() % 100;
			float fpAccum = 0;
			unsigned int uiFsmSize = (unsigned int)_fsm[_state][event].size();

			for(unsigned int i = 0; i < uiFsmSize; i++) 
			{
				if(iRand < _fsm[_state][event][i]->fpProbability + fpAccum) 
				{
					_state = _fsm[_state][event][i]->uiDestinationState;

					return;
				}

				fpAccum += _fsm[_state][event][i]->fpProbability;
			}
		}
	}

	void SetState(int state)
	{
		_state = state;
	}

	void SetUpdateFunc(int state, UpdateFuncPointer func)
	{ 
		_updateFuncs[state] = func; 
	}

	void SetDrawFunc(int state, DrawFuncPointer func)
	{ 
		_drawFuncs[state] = func;
	}

	int GetState()
	{ 
		return _state; 
	}

	void Draw();
	bool Update();
	bool ShutDown();

private:

	int						_state;
	std::vector<sState*>	**_fsm;
	int						_stateCount;

	DrawFuncPointer			*_drawFuncs;
	UpdateFuncPointer		*_updateFuncs;
};
//-----------------------------------------------------------------------------------
#endif
//-----------------------------------------------------------------------------------